<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <!-- <a-button type="primary" @click="handleAdjust({})" v-auth="'mes:scOrder:jhtz'">
          <Icon icon="ant-design:clock-circle-outlined" /> {{ t('计划调整') }}
        </a-button> -->
        <a-button type="primary" @click="handlecxfp({})" v-auth="'mes:orderLinefp:cxfp'">
          <Icon icon="simple-line-icons:settings" /> {{ t('产线分配') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.code }}
        </a>
      </template>
    </BasicTable>
    <!-- <InputForm @register="registerDrawer" @success="handleSuccess" /> -->
    <formTabs @register="registerDrawer" @success="handleSuccess" />
    <AdjustModal @register="registerModal" @success="handleSuccess" />
    <cxfpModal @register="registercxfpDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsMoOrderScOrderList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, onMounted } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { scOrderDelete, scOrderListData } from '/@/api/mes/mo/order/scOrder';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import { useModal } from '/@/components/Modal';
  // import InputForm from './form.vue';
  import formTabs from './formTabs.vue';
  import AdjustModal from './adjustForm.vue';
  import cxfpModal from './cxfpForm.vue';
  import { basWorkShopTreeData } from '/@/api/bas/mes/workshop/basMesWorkShop';

  const { t } = useI18n('mo.order.scOrder');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('生产订单管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('订单号'),
        field: 'code',
        component: 'Input',
      },
      {
        label: t('订单日期'),
        field: 'ddate',
        component: 'RangePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          showTime: false,
        },
      },
      {
        label: t('存货编码'),
        field: 'invCode',
        component: 'Input',
      },
      {
        label: t('存货名称'),
        field: 'basInv.cinvname',
        component: 'Input',
      },
      {
        label: t('分配结果'),
        field: 'fpStatus',
        component: 'Select',
        componentProps: {
          dictType: 'mes_mo_order_fp_status',
        },
      },
      {
        label: t('订单状态'),
        field: 'orderStatusIn',
        component: 'Select',
        componentProps: {
          dictType: 'mes_mo_order_status',
          mode: 'multiple',
        },
      },
      {
        label: t('归属车间'),
        field: 'workShop.officeName',
        fieldLabel: 'workShop.officeName',
        component: 'TreeSelect',
        componentProps: {
          api: basWorkShopTreeData,
          allowClear: true,
          canSelectParent: false,
        },
      },
    ],
    fieldMapToTime: [['ddate', ['ddate_gte', 'ddate_lte']]],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('订单号'),
      dataIndex: 'code',
      key: 'a.code',
      sorter: true,
      width: 150,
      align: 'left',
      slot: 'firstColumn',
      fixed: 'left',
    },
    {
      title: t('行号'),
      dataIndex: 'sortNum',
      key: 'a.sort_num',
      sorter: true,
      width: 80,
      align: 'center',
      fixed: 'left',
    },
    {
      title: t('计划开始(时间)'),
      dataIndex: 'planStartDateWork',
      key: 'a.plan_start_date_work',
      sorter: true,
      width: 130,
      align: 'center',
      fixed: 'left',
    },
    {
      title: t('计划结束(时间)'),
      dataIndex: 'planOverDateWork',
      key: 'a.plan_over_date_work',
      sorter: true,
      width: 130,
      align: 'center',
      fixed: 'left',
    },
    {
      title: t('存货编码'),
      dataIndex: 'invCode',
      key: 'a.inv_code',
      sorter: true,
      width: 130,
      align: 'left',
      fixed: 'left',
    },
    {
      title: t('存货名称'),
      dataIndex: 'basInv.cinvname',
      key: 'inv.cinvname',
      sorter: true,
      width: 280,
      align: 'left',
    },
    {
      title: t('数量'),
      dataIndex: 'iqty',
      key: 'a.iqty',
      sorter: true,
      width: 100,
      align: 'right',
    },
    {
      title: t('单位'),
      dataIndex: 'basInv.ccomunitname',
      key: 'inv.ccomunitname',
      sorter: true,
      width: 100,
      align: 'right',
    },
    // {
    //   title: t('计划开始'),
    //   dataIndex: 'planStartDate',
    //   key: 'a.plan_start_date',
    //   sorter: true,
    //   width: 130,
    //   align: 'center',
    // },
    // {
    //   title: t('计划结束'),
    //   dataIndex: 'planOverDate',
    //   key: 'a.plan_over_date',
    //   sorter: true,
    //   width: 130,
    //   align: 'center',
    // },
    
    {
      title: t('归属车间'),
      dataIndex: 'workShop.officeName',
      key: 'a.work_shop_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('累计分配'),
      dataIndex: 'sumFpQty',
      key: 'a.sum_fp_qty',
      sorter: true,
      width: 100,
      align: 'right',
    },
    // {
    //   title: t('剩余分配'),
    //   dataIndex: 'sumFpQty',
    //   key: 'a.sum_fp_qty',
    //   sorter: true,
    //   width: 100,
    //   align: 'right',
    // },

    {
      title: t('完工数'),
      dataIndex: 'sumOverQty',
      key: 'a.sum_over_qty',
      sorter: true,
      width: 100,
      align: 'right',
    },
    {
      title: t('合格数'),
      dataIndex: 'sumOkQty',
      key: 'a.sum_ok_qty',
      sorter: true,
      width: 100,
      align: 'right',
    },
    {
      title: t('订单日期'),
      dataIndex: 'ddate',
      key: 'a.ddate',
      sorter: true,
      width: 120,
      align: 'center',
    },
    {
      title: t('规格型号'),
      dataIndex: 'basInv.cinvstd',
      key: 'inv.cinvstd',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('分派状态'),
      dataIndex: 'fpStatus',
      key: 'a.fp_status',
      sorter: true,
      width: 90,
      align: 'center',
      dictType: 'mes_mo_order_fp_status',
      fixed: 'right',
    },
    {
      title: t('订单状态'),
      dataIndex: 'orderStatus',
      key: 'a.order_status',
      sorter: true,
      width: 90,
      align: 'center',
      dictType: 'mes_mo_order_status',
      fixed: 'right',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 240,
    actions: (record: Recordable) => [
      // {
      //   icon: 'clarity:note-edit-line',
      //   title: t('编辑生产订单'),
      //   onClick: handleForm.bind(this, { id: record.id }),
      //   auth: 'mo:order:scOrder:edit',
      // },
      // {
      //   icon: 'ant-design:delete-outlined',
      //   color: 'error',
      //   title: t('删除生产订单'),
      //   popConfirm: {
      //     title: t('是否确认删除生产订单'),
      //     confirm: handleDelete.bind(this, { id: record.id }),
      //   },
      //   auth: 'mo:order:scOrder:edit',
      // },
      {
        // icon: 'clarity:note-edit-line',
        label: t('分配详情'),
        title: t('分配详情'),
        onClick: handleForm.bind(this, { id: record.id, activeKey: '3' }),
        auth: 'mo:order:scOrder:edit',
      },
      {
        // icon: 'clarity:note-edit-line',
        label: t('查看工艺'),
        title: t('查看工艺'),
        onClick: handleForm.bind(this, { id: record.id, activeKey: '2' }),
        auth: 'mo:order:scOrder:edit',
      },
      {
        // icon: 'clarity:note-edit-line',
        label: t('任务队列'),
        title: t('任务队列'),
        onClick: handleForm.bind(this, { id: record.id, activeKey: '4' }),
        auth: 'mo:order:scOrder:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRowKeys, getSelectRows }] = useTable({
    api: scOrderListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  const [registerModal, { openModal }] = useModal();
  // const [registercxfpModal, { openModal:opencxfpModal }] = useModal();

  const [registercxfpDrawer, { openDrawer: opencxfpDrawer }] = useDrawer();

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await scOrderDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  // 计划调整
  async function handleAdjust() {
    if (getSelectRowKeys().length) {
      openModal(true, { selIds: getSelectRowKeys().join(',') });
      handleSuccess();
    } else {
      showMessage('请选择一行数据！！');
    }
  }
  // 产线分配
  async function handlecxfp() {
    if (getSelectRows().length) {
      //const res = await scOrderAdjust({ selIds: getSelectRows().join(',') });
      //showMessage(res.message);
      //handleSuccess();
      opencxfpDrawer(true, { arr: getSelectRows() });
    } else {
      showMessage('请选择一行数据！！');
    }
  }

  function handleSuccess() {
    reload();
  }

  onMounted(async () => {
    handleSearch();
  });

  async function handleSearch() {
    await getForm().setFieldsValue({
      orderStatusIn: '1,2,3',
    });
    reload();
    await getForm().setFieldsValue({
      orderStatusIn: '1,2,3',
    });
  }
</script>
